<?php
/**
 *    @version [ Masterton ]
 *    @package hwdVideoShare
 *    @copyright (C) 2007 - 2009 Highwood Design
 *    @license Creative Commons Attribution-Non-Commercial-No Derivative Works 3.0 Unported Licence
 *    @license http://creativecommons.org/licenses/by-nc-nd/3.0/
 */
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

global $mainframe, $option, $smartyvs;

if (!file_exists(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS)) {
	echo "<div style=\"border:1px solid #c30;color:#c30;margin: 0 0 5px 0;padding: 5px;font-weight: bold;text-align:left;text-align:center;\">The hwdVideoShare component is not installed, you can not use this module.</div>"; return;
}

$db =& JFactory::getDBO();
$my = & JFactory::getUser();
require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS.'helpers'.DS.'initialise.php');
hwdvsInitialise::coreRequire();
hwdvsInitialise::language();
hwdvsInitialise::template();
hwdvsInitialise::mysqlQuery();
hwdvsInitialise::definitions();

if (@_HWD_VS_PLUGIN_COMPS !== 214) {
	echo "<div style=\"border:1px solid #c30;color:#c30;margin: 0 0 5px 0;padding: 5px;font-weight: bold;text-align:left;text-align:center;\">This module is not compatible with your version of hwdVideoShare.</div>"; return;
}

$c = hwd_vs_Config::get_instance();
$video_id = JRequest::getInt( 'video_id', null );

/* Security Note: These values are auto-sanitized by mosGetParam() */
$hwdvids_params['mod_hwd_itemid'] 	= (int)$params->get( 'mod_hwd_itemid', '0');
$hwdvids_params['mod_style'] 	    = (int)$params->get( 'mod_style', 0);
$hwdvids_params['mod_subs'] 	    = (int)$params->get( 'mod_subs', 0);
$hwdvids_params['mod_link'] 	    = (int)$params->get( 'mod_link', 1);
$hwdvids_params['mod_hideempty'] 	= (int)$params->get( 'mod_hideempty', 0);
$hwdvids_params['mod_ordering']     = $params->get( 'mod_ordering', "nameASC");
$hwdvids_params['mod_showvcount']   = $params->get( 'mod_showvcount', 0);

if ($hwdvids_params['mod_hwd_itemid'] == 0) {
	$hwdvids_params['mod_hwd_itemid'] = hwd_vs_tools::generateValidItemid();
}

$where = "\n WHERE a.published = 1";
if ($hwdvids_params['mod_hideempty'] == 1) {
	$where.= "\n AND num_vids > 0";
}

if ($hwdvids_params['mod_ordering'] == "orderASC") {
	$order = "\n ORDER BY a.ordering ASC";
} else if ($hwdvids_params['mod_ordering'] == "orderDESC") {
	$order = "\n ORDER BY a.ordering DESC";
} else if ($hwdvids_params['mod_ordering'] == "nameASC") {
	$order = "\n ORDER BY a.category_name ASC";
} else if ($hwdvids_params['mod_ordering'] == "nameDESC") {
	$order = "\n ORDER BY a.category_name DESC";
} else if ($hwdvids_params['mod_ordering'] == "novidsASC") {
	$order = "\n ORDER BY a.novids ASC";
} else if ($hwdvids_params['mod_ordering'] == "novidsDESC") {
	$order = "\n ORDER BY a.novids DESC";
} else if ($hwdvids_params['mod_ordering'] == "nosubsASC") {
	$order = "\n ORDER BY a.nosubs ASC";
} else if ($hwdvids_params['mod_ordering'] == "nosubsDESC") {
	$order = "\n ORDER BY a.nosubs DESC";
}

$query = "SELECT a.*"
		. "\n FROM #__hwdvidscategories AS a"
		. $where
		. "\n AND a.parent = 0"
		. $order
		;

$db->SetQuery( $query );
$rows = $db->loadObjectList();

$list = array();
$counter = 0;
for ($i=0, $n=count($rows); $i < $n; $i++) {
	$row = $rows[$i];
	if ($hwdvids_params['mod_link']  == 1) {
		$list[$counter]->link = hwd_vs_tools::generateCategoryLink($row->id, $row->category_name, $hwdvids_params['mod_hwd_itemid']);
	} else {
		$list[$counter]->link = $row->category_name;
	}

	if ($hwdvids_params['mod_showvcount'] == 1) {
		$list[$counter]->link.= " (".$row->num_vids." "._HWDVIDS_VIDEOS.")";
	}

	$counter++;

	if ($hwdvids_params['mod_subs'] == 1) {

		$query = "SELECT a.*"
				. "\n FROM #__hwdvidscategories AS a"
				. $where
				. "\n AND a.parent = ".$row->id
				. $order
				;

		$db->SetQuery( $query );
		$subs = $db->loadObjectList();

		for ($j=0, $m=count($subs); $j < $m; $j++) {
			$sub = $subs[$j];
			if ($hwdvids_params['mod_link']  == 1) {
				$list[$counter]->link = '....'.hwd_vs_tools::generateCategoryLink($sub->id, $sub->category_name, $hwdvids_params['mod_hwd_itemid']);
			} else {
				$list[$counter]->link = '....'.$sub->category_name;
			}

			if ($hwdvids_params['mod_showvcount'] == 1) {
				$list[$counter]->link.= "(".$row->category_name." Videos)";
			}

			$counter++;

		}

	}

}
$smartyvs->assign("list", $list);
$smartyvs->assign("hwdvids_params", $hwdvids_params);

$smartyvs->display('mod_hwd_vs_categories.tpl');
return;
?>
